package com.lms.servlet.user;

import com.alibaba.fastjson.JSONArray;
import com.lms.bean.Root;
import com.lms.bean.User;
import com.lms.service.root.RootService;
import com.lms.service.root.RootServiceImpl;
import com.lms.service.user.UserService;
import com.lms.service.user.UserServiceImpl;
import com.lms.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if ("getRootMsg".equals(req.getParameter("method"))){
            Map<String,Object> msg=new HashMap<>();
            User user=(User)(req.getSession().getAttribute(Constants.USER_SESSION));
            Root root=(Root) (req.getSession().getAttribute(Constants.ROOT_SESSION));
            if (user==null && root==null){
                msg.put("haveSession","no");
                UserService userService = new UserServiceImpl();
                msg.put("rootMsg",userService.getRootMsg());
            }else {
                msg.put("haveSession","yes");
                if (user!=null){
                    msg.put("from",user.getUserRole());
                    msg.put("pwd",user.getUserPassWord());
                    msg.put("role",user.getUserRole());
                }else {
                    msg.put("from",2);
                    msg.put("pwd",root.getUserPassWord());
                }
            }

            /*写出JSON*/
            resp.setContentType("application/json");
            PrintWriter writer = resp.getWriter();
            writer.write(JSONArray.toJSONString(msg));
            writer.flush();
            writer.close();
        }else {
            String user,pwd,root,rootPwd,role="";
            User loginUser=null;
            Root loginRoot=null;
            if ("user".equals(req.getParameter("local"))){
                user=req.getParameter("userCode");
                pwd=req.getParameter("userPassword");
                UserService userService = new UserServiceImpl();
                loginUser = userService.login(user, pwd);
                role="user";
            }else if ("root".equals(req.getParameter("local"))){
                root=req.getParameter("rootCode");
                rootPwd=req.getParameter("rootPassword");
                RootService rootService=new RootServiceImpl();
                loginRoot = rootService.login(root, rootPwd);
                role="root";
            }


            if (loginUser!=null || loginRoot!=null){
                /*学生端，管理员端session分离*/
                if ("root".equals(role)){
                    req.getSession().setAttribute(Constants.ROOT_SESSION,loginRoot);
                }else if ("user".equals(role)){
                    req.getSession().setAttribute(Constants.USER_SESSION,loginUser);
                }
                resp.sendRedirect("jsp/user.do?method=getLabList&local=introduce&from="+role);
            }else {
                if ("user".equals(role)){
                    req.setAttribute("error","用户名或密码不正确");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }else if ("root".equals(role)){
                    req.setAttribute("error2","用户名或密码不正确");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }else{
                    req.setAttribute("error","发生异常错误");
                    req.setAttribute("error2","发生异常错误");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

}
